/*
 * main.c
 *
 *  Created on: Jun 8, 2010
 *      Author: iandrono
 */

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include "bluetooth/bluetooth.h"
#include "leds/leds.h"
#include "acelerometer/acelerometer.h"
#include "timer/timer.h"
#include "defines.h"
#include "uart/uart.h"
#include "adc/adc.h"

unsigned char delayms;

int main( void ) {

	initLeds();
	initUart();
	initADC();
	initAcelerometer();
	initTimer();

	//Imagen mockeada:
	imageType my_image;
	ROWS_Q = 11;
	unsigned char intImage[3][11][16] = {
			//Imagen en R
			{
					{0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0},
					{0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0},
					{0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0},
					{0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0},
					{0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0},
					{0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0},
					{0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0},
					{0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0},
					{0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0}
			},
			//Imagen en G
			{
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
			},
			//Imagen en B
			{
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
					{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
			}
	};

	unsigned char powersOf2[BYTE_SIZE] = {128,64,32,16,8,4,2,1};
	int dim, acum, i, j, k, p;
	for( k=0 ; k<COLORS_Q_MAX ; k++ ) {
		for( i=0 ; i<ROWS_Q ; i++ ) {
			for( j=0,dim=0 ; j<LEDS_Q_MAX/BYTE_SIZE ; j++ ) {
				for( p=0,acum=0 ; p<BYTE_SIZE ; p++,dim++ ) {
					acum += (intImage[k][i][dim] * powersOf2[p]);
				}
				my_image[k][i][j] = acum;
			}
		}
	}

	resetVideoMemory(&my_image);
	startSequence();
	sei();

	while ( TRUE ) {
		#if WITH_ACEL
		acelerometerAdjust();
		#endif
	}
}
